package org.dromara.wx.eventHandle;


import com.github.sd4324530.fastweixin.api.config.ApiConfig;
import com.github.sd4324530.fastweixin.handle.EventHandle;
import com.github.sd4324530.fastweixin.message.BaseMsg;
import com.github.sd4324530.fastweixin.message.TextMsg;
import com.github.sd4324530.fastweixin.message.req.BaseEvent;
import com.github.sd4324530.fastweixin.message.req.EventType;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.web.service.SysWxService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Created by fly on 2016/6/1.
 */
@RequiredArgsConstructor
@Slf4j
@Component
public class UserEventHandler implements EventHandle<BaseEvent> {

    private static Logger logger = LoggerFactory.getLogger(UserEventHandler.class);

    private final SysWxService sysWxService;


    @Override
    public BaseMsg handle(final BaseEvent event, final ApiConfig config) {
        if (event.getEvent().equals(EventType.SUBSCRIBE)) {
            sysWxService.updateMpOpenIdToUser(config.getAppid(), event.getFromUserName());
        } else {
            return new TextMsg("再见");
        }
        return new TextMsg("");
    }

    @Override
    public boolean beforeHandle(BaseEvent event, ApiConfig config) {
        if (event.getEvent().equals(EventType.SUBSCRIBE) || event.getEvent().equals(EventType.UNSUBSCRIBE))
            return true;
        return false;
    }
}
